package com.htjc.integral.redpacket.service.impl;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.commons.service.util.httpClient.HttpClientTools;
import com.htjc.integral.common.enums.JdReturn;
import com.htjc.integral.common.util.CommonUtil;
import com.htjc.integral.common.util.Constance;
import com.htjc.integral.common.util.MD5Util;
import com.htjc.integral.model.redpacket.BpRedPacInv;
import com.htjc.integral.model.redpacket.EcBpRedOrder;
import com.htjc.integral.model.redpacket.EcBpRedOrderExchange;
import com.htjc.integral.redpacket.mapper.EcBpRedOrderMapper;
import com.htjc.integral.redpacket.mapper.EcBpRedPacInvMapper;

import cfca.sadk.util.Base64;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;

@Slf4j(topic="redpacketLogger")
@Service("buyElecRedPacketForExtSysService")
public class BuyElecRedPacketForExtSysService  {
	@Autowired
	private RedPacketForExtSysService redPacketForExtSysService;
	@Autowired
	private EcBpRedOrderMapper ecBpRedOrderMapper;
	@Autowired
	private EcBpRedPacInvMapper ecBpRedPacInvMapper;
	
	/**
	 * 通知商户下单接口
	 */
	public String notifyOrder(String req) {
		log.info("接收到的请求密文为：" + req);
		long startTime = System.currentTimeMillis();
		log.info("开始进入时间：startTime=0");
		
		JSONObject resultJo = new JSONObject();
		String[] inputStr = { "source", "version", "identity_id", "data" };
		String[] outputStr = { "code", "msg" };
		String[] rightKeys = { "orderId", "phone", "itemId", "title", "price", "quantity", "finalFee" };
		try {
			/* 1.入参校验 */
			Map<String, Object> retMap = checkReq(req, inputStr, outputStr, rightKeys, true);
			if ("false".equals(retMap.get("isSuccess").toString())) {
				return retMap.get("msg").toString();
			}
			/* 2. 记录订单数据 */
			JSONObject jo = (JSONObject) retMap.get("msg");
			String identityId = jo.getString(inputStr[2]);// 合作商户号
			JSONObject dataBody = jo.getJSONObject(inputStr[3]); // 请求体
			EcBpRedOrder redOrder = (EcBpRedOrder) JSONObject.toBean(dataBody, EcBpRedOrder.class);
			redOrder.setPlatformId(Constance.CMCC_PLATFORM_ID);
			redOrder.setStatus("0");
			redOrder.setIdentityId(identityId);
			
			EcBpRedOrder redOrderO = ecBpRedOrderMapper.selectOrderInformationByID(redOrder.getOrderId());
			if(redOrderO != null ) {//yang-jc 去掉【 && "1".equals(redOrderO.getStatus())】 业务逻辑bug  status 一开始不等1
				resultJo.put(outputStr[0], JdReturn.operFail.getCode());
				resultJo.put(outputStr[1], JdReturn.operFail.getMsg());
				log.info("==================调用通知商户下单接口end【重复提交订单："+redOrder.getOrderId()+"】==================");
				return resultJo.toString();
			}else if(redOrderO == null){
				ecBpRedOrderMapper.insertEcBpRedOrder(redOrder);
			}
			/* 3. 派发红包 */
			// 查红包库存id
			String stockNumberId = ecBpRedOrderMapper.loadStockNumberId(redOrder.getItemId());
			if (StringUtils.isBlank(stockNumberId)) {
				resultJo.put(outputStr[0], JdReturn.operFail.getCode());
				resultJo.put(outputStr[1], JdReturn.operFail.getMsg());
				log.info("==================调用通知商户下单接口end【没有平台" + redOrder.getPlatformId() + "的红包】==================");
				return resultJo.toString();
			}
			redOrder.setStockNumberId(stockNumberId);
			
			long middleTime = System.currentTimeMillis();
			log.info("调用电e宝之前的时间：middleTime=" + (middleTime - startTime));
			
			String isSuccess = payoutRedPacket(redOrder);
			/* 4. 红包派发成功后提交虚拟码 */
			if ("1".equals(isSuccess)) {
				// 修改订单状态为成功
				ecBpRedOrderMapper.updateRedOrderStatus(redOrder.getOrderId());
				// 提交虚拟码(调用移动接口)
				String retJson = setVirtualCode(redOrder.getOrderId());
				JSONObject json = JSONObject.fromObject(retJson);
				// 记录接口调用信息
				Date modifyTime = new Date();
				String retCode = json.get("code").toString();
				addEcBpRedOrderExchange("0".equals(retCode) ? "1" : "0", redOrder.getOrderId(), Constance.SUBMIT_VIRTUAL_CODE,
						modifyTime);
				/* 5. 提交虚拟码成功后提交消费记录 */
				if ("0".equals(retCode)) {// 提交虚拟码成功
					// 查询红包批次号和面额
					List<Map<String, Object>> redPacPlatform1 = ecBpRedOrderMapper
							.loadRedPacPlatform(redOrder.getPlatformId());
					if (redPacPlatform1 == null) {
						resultJo.put(outputStr[0], JdReturn.operFail.getCode());
						resultJo.put(outputStr[1], JdReturn.operFail.getMsg());
						log.info("==================调用通知商户下单接口end【" + resultJo.toString() + "】==================");
						return resultJo.toString();
					}
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("code", redOrder.getItemId());
					log.info("商品id===="+redOrder.getItemId());
					BpRedPacInv redPacPlatform = ecBpRedPacInvMapper
							.selectByCode(map);//查询面额
					// 提交消费记录(调用移动接口)
					String result = callSetRecord(redOrder, modifyTime, redPacPlatform);
					// 记录接口调用信息
					JSONObject jsonO = JSONObject.fromObject(result);
					addEcBpRedOrderExchange("0".equals(jsonO.get("code").toString()) ? "1" : "0", redOrder.getOrderId(),
							Constance.SUBMIT_SETRECORD, new Date());
					
					long endTime = System.currentTimeMillis();
					log.info("调用完成时间：endTime=" + (endTime - startTime));
				}
			} else {
				resultJo.put(outputStr[0], JdReturn.operFail.getCode());
				resultJo.put(outputStr[1], JdReturn.operFail.getMsg());
				return resultJo.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultJo.put(outputStr[0], JdReturn.operFail.getCode());
			resultJo.put(outputStr[1], JdReturn.operFail.getMsg());
			log.info("==================调用通知商户下单接口end【" + resultJo.toString() + "】==================");
			return resultJo.toString();
		}
		resultJo.put(outputStr[0], JdReturn.success.getCode());
		resultJo.put(outputStr[1], JdReturn.success.getMsg());
		return resultJo.toString();
	}

	/**
	 * 提交消费记录
	 * 
	 * @param redOrder
	 * @param date
	 * @param redPacPlatform
	 * @return
	 */
	private String callSetRecord(EcBpRedOrder redOrder, Date date, BpRedPacInv redPacPlatform) {
		String redpacId = redPacPlatform.getId().toString();
		String denomination = redPacPlatform.getDenomination().toString();
		log.info("redpacId==========:" + redpacId);
		log.info("denomination======:" + denomination);
		
		String picihao = redOrder.getItemId();
		String orderId = redOrder.getOrderId();
		String codeID = redOrder.getIdentityId() + "_" + orderId + picihao;
		log.info("虚拟码号codeID======:" + codeID);
		
		List<Map<String, Object>> recordList = new ArrayList<Map<String, Object>>();
		Map<String, Object> recordMap = new HashMap<String, Object>();
		Map<String, Object> recordListMap = new HashMap<String, Object>();
		recordMap.put("orderId", redOrder.getOrderId());
		recordMap.put("itemId", redOrder.getItemId());
		recordMap.put("useId", codeID);// 建议使用----商户号_红包批次id，红包的是按批次单一生成的，避免重复。
		recordMap.put("virtualCode", codeID);// 虚拟码
		recordMap.put("virtualCodePass", codeID);// 商城红包没有卡密，卡密设置为虚拟码流水号
		recordMap.put("useAmount", denomination);
		recordMap.put("useDatetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
		recordMap.put("useContent", denomination + "元电费红包");
		recordMap.put("phone", redOrder.getPhone());
		recordList.add(recordMap);
		Map<String, Object> jsonheaher = new HashMap<String, Object>();
		jsonheaher.put("version", "2.0");
		jsonheaher.put("identity_id", redOrder.getIdentityId());
		jsonheaher.put("source", "1");
		recordListMap.put("recordList", recordList);
		jsonheaher.put("data", recordListMap);
		String url = this.ecBpRedPacInvMapper.getParaValue("site.mall.red.cmcc")+ "setRecord";
		String jsonReq = JSON.toJSONString(jsonheaher);
		log.info("=====提交消费记录接口入参=====：" + jsonReq);
		// 加密req
		String reqr = getEncodeReqStr(jsonReq);
		Map<String, String> reqMap = new HashMap<String, String>();
		reqMap.put("req", reqr);
		log.info("调用远程地址:" + url);
		String result = HttpClientTools.getInstance().includeByPostTo60(url, reqMap);
		log.info("接口返回结果:" + result);
		return result;
	}

	private String getEncodeReqStr(String reqStr) {
		char[] chs = reqStr.toCharArray();
		Arrays.sort(chs);
		String sortStr = new String(chs);
		String secretKey = this.ecBpRedPacInvMapper.getParaValue("site.mall.red.cmcc.secretKey");
		String sign_m = MD5Util.getSign(secretKey + sortStr);
		// String ret = com.sgcc.service.util.vip.Base64.encode((sign_m +
		// reqStr).getBytes());
		byte[] retByte = Base64.encode((sign_m + reqStr).getBytes());
		String ret = new String(retByte);
		return ret;
	}

	/**
	 * 记录接口调用信息
	 * 
	 * @param status
	 * @param orderId
	 * @param interfaceName
	 */
	private void addEcBpRedOrderExchange(String status, String orderId, String interfaceName, Date date) {
		EcBpRedOrderExchange redOrderEx = new EcBpRedOrderExchange();
		redOrderEx.setStatus(status);
		redOrderEx.setOrderId(orderId);
		redOrderEx.setModifyTime(date);
		redOrderEx.setInterfaceName(interfaceName);
		ecBpRedOrderMapper.insertEcBpRedOrderExchange(redOrderEx);
	}

	/**
	 * 提交虚拟码方法
	 * 
	 * @param orderId
	 *            订单号
	 * @param itemId
	 *            商品ID
	 * @param picihao
	 *            批次号
	 * @return
	 */
	public String setVirtualCode(String orderId) {
		log.info("========接收到的订单：" + orderId + "=========");
		String url = this.ecBpRedPacInvMapper.getParaValue("site.mall.red.cmcc")+ "setVirtualCode";
		EcBpRedOrder ecBpRedOrder = ecBpRedOrderMapper.selectOrderInformationByID(orderId);
		
		String picihao = ecBpRedOrder.getItemId();
		String orderId1 = ecBpRedOrder.getOrderId();
		Map<String, Object> virtualCodes = new HashMap<String, Object>();
		String codeID = ecBpRedOrder.getIdentityId() + "_" + orderId1 + picihao;// 虚拟码流水号：商户号开头加虚拟码流水号，中间用下划线分隔
		log.info("虚拟码号=========codeID"+codeID);
		virtualCodes.put("vcode", codeID);
		virtualCodes.put("vcodePass", codeID);// 商城红包没有卡密，卡密设置为虚拟码流水号
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		list.add(virtualCodes);
		Map<String, Object> jsonheaher = new HashMap<String, Object>();
		jsonheaher.put("version", "2.0");
		jsonheaher.put("identity_id", ecBpRedOrder.getIdentityId());
		jsonheaher.put("source", "1");
		Map<String, Object> jsonData = new HashMap<String, Object>();
		jsonData.put("orderId", orderId);
		jsonData.put("itemId", ecBpRedOrder.getItemId());
		jsonData.put("virtualCodes", list);
		jsonheaher.put("data", jsonData);
		String result = "";
		try {
			String jsonReq = JSON.toJSONString(jsonheaher);
			log.info("=====提交虚拟码接口入参=====：" + jsonReq);
			// 加密req
			String req = getEncodeReqStr(jsonReq);
			Map<String, String> reqMap = new HashMap<String, String>();
			reqMap.put("req", req);
			log.info("调用远程地址:" + url);
			result = HttpClientTools.getInstance().includeByPostTo60(url, reqMap);
			log.info("调用提交虚拟码接口返回结果：" + result);
		} catch (Exception e) {
			log.info("=====提交虚拟码接口调用移动积分平台失败=====");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 重发虚拟码接口
	 */
	public String resendVirtualCode(String req) {
		log.info("接收到的请求密文为：" + req);
		JSONObject resultJo = new JSONObject();
		String[] inputStr = { "source", "version", "identity_id", "data" };
		String[] outputStr = { "code", "msg" };
		String[] rightKeys = { "orderId" };
		String result = "";
		try {
			// 入参校验 
			Map<String, Object> retMap = checkReq(req, inputStr, outputStr, rightKeys, false);
			if ("false".equals(retMap.get("isSuccess").toString())) {
				return retMap.get("msg").toString();
			}
			JSONObject jo = (JSONObject) retMap.get("msg");
			JSONObject dataBody = jo.getJSONObject(inputStr[3]); // 请求体
			String orderId = dataBody.getString("orderId");
			if (!StringUtils.isBlank(orderId)) {
				Map<String, Object> OrderMap = new HashMap<String, Object>();
				OrderMap.put("orderId", orderId);
				OrderMap.put("interfaceName", Constance.SUBMIT_VIRTUAL_CODE);
				EcBpRedOrderExchange ecBpRedOrderExchange = ecBpRedOrderMapper.selectExchangeByID(OrderMap);
				EcBpRedOrder redOrder = ecBpRedOrderMapper.selectByBpRedOrderId(orderId);
				if (ecBpRedOrderExchange != null) {
					// 提交虚拟码
					String isSucc = setVirtualCode(orderId);
					JSONObject json = JSONObject.fromObject(isSucc);
					if ("0".equals(json.get("code").toString())) {// 提交虚拟码成功
						boolean flag = false;
						if ("0".equals(ecBpRedOrderExchange.getStatus())) {
							flag = true;
							updateEcBpRedOrderExchange("1", orderId, Constance.SUBMIT_VIRTUAL_CODE, new Date());// 修改提交虚拟码记录为成功状态
						}
						List<Map<String, Object>> redPacPlatform1 = ecBpRedOrderMapper
								.loadRedPacPlatform(redOrder.getPlatformId());// 查询红包批次号和面额
						if (redPacPlatform1 == null) {
							resultJo.put(outputStr[0], JdReturn.operFail.getCode());
							resultJo.put(outputStr[1], JdReturn.operFail.getMsg());
							log.info("==================调用通知商户下单接口end【" + resultJo.toString() + "】==================");
							return resultJo.toString();
						}
						// 提交消费记录
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("code", redOrder.getItemId());
						BpRedPacInv redPacPlatform = ecBpRedPacInvMapper
								.selectByCode(map);
						result = callSetRecord(redOrder, new Date(), redPacPlatform);
						// 记录接口调用信息
						JSONObject jsonO = JSONObject.fromObject(result);
						if (flag) {
							addEcBpRedOrderExchange("0".equals(jsonO.get("code").toString()) ? "1" : "0",
									redOrder.getOrderId(), Constance.SUBMIT_SETRECORD, new Date());
						} else {
							updateEcBpRedOrderExchange("0".equals(jsonO.get("code").toString()) ? "1" : "0", orderId,
									Constance.SUBMIT_SETRECORD, new Date());
						}
					} else {
						resultJo.put(outputStr[0], JdReturn.platformError.getCode());
						resultJo.put(outputStr[1], JdReturn.platformError.getMsg());
						log.info("==================调用重发虚拟码接口end【" + resultJo.toString() + "】==================");
						return resultJo.toString();
					}
				} else {
					// 派发红包
					String stockNumberId = ecBpRedOrderMapper.loadStockNumberId(redOrder.getItemId());// 查红包库存id
					if (StringUtils.isBlank(stockNumberId)) {
						resultJo.put(outputStr[0], JdReturn.operFail.getCode());
						resultJo.put(outputStr[1], JdReturn.operFail.getMsg());
						log.info("==================调用通知商户下单接口end【没有平台" + redOrder.getPlatformId()
								+ "的红包】==================");
						return resultJo.toString();
					}
					redOrder.setStockNumberId(stockNumberId);
//					String useflag = "1";
					String isSuccess = payoutRedPacket(redOrder);
					if (isSuccess.equals("1")) {
						// 修改订单状态为成功
						ecBpRedOrderMapper.updateRedOrderStatus(redOrder.getOrderId());
						// 提交虚拟码
						String retJson = setVirtualCode(redOrder.getOrderId());
						JSONObject json = JSONObject.fromObject(retJson);
						// 记录接口调用信息
						Date modifyTime = new Date();
						String retCode = json.get("code").toString();
						addEcBpRedOrderExchange("0".equals(retCode) ? "1" : "0", redOrder.getOrderId(),
								Constance.SUBMIT_VIRTUAL_CODE, modifyTime);
						/* 提交虚拟码成功后提交消费记录 */
						if ("0".equals(retCode)) {// 提交虚拟码成功
							// 查询红包批次号和面额
							List<Map<String, Object>> redPacPlatform1 = ecBpRedOrderMapper
									.loadRedPacPlatform(redOrder.getPlatformId());
							if (redPacPlatform1 == null) {
								resultJo.put(outputStr[0], JdReturn.operFail.getCode());
								resultJo.put(outputStr[1], JdReturn.operFail.getMsg());
								log.info("==================调用通知商户下单接口end【" + resultJo.toString()
										+ "】==================");
								return resultJo.toString();
							}
							Map<String, Object> map = new HashMap<String, Object>();
							map.put("code", redOrder.getItemId());
							BpRedPacInv redPacPlatform = ecBpRedPacInvMapper
									.selectByCode(map);
							result = callSetRecord(redOrder, modifyTime, redPacPlatform);
							// 记录接口调用信息
							JSONObject jsonO = JSONObject.fromObject(result);
							addEcBpRedOrderExchange("0".equals(jsonO.get("code").toString()) ? "1" : "0",
									redOrder.getOrderId(), Constance.SUBMIT_SETRECORD, new Date());
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultJo.put(outputStr[0], JdReturn.operFail.getCode());
			resultJo.put(outputStr[1], JdReturn.operFail.getMsg());
			log.info("==================调用重发虚拟码接口end【" + resultJo.toString() + "】==================");
			return resultJo.toString();
		}
		resultJo.put(outputStr[0], JdReturn.success.getCode());
		resultJo.put(outputStr[1], JdReturn.success.getMsg());
		return resultJo.toString();
	}

	private void updateEcBpRedOrderExchange(String status, String orderId, String interfaceName, Date date) {
		Map<String, Object> InterFaceStatus = new HashMap<String, Object>();
		InterFaceStatus.put("orderId", orderId);
		InterFaceStatus.put("interfaceName", interfaceName);
		InterFaceStatus.put("status", status);
		InterFaceStatus.put("modifyTime", date);
		ecBpRedOrderMapper.updateInterFaceStatus(InterFaceStatus);
	}

	/**
	 * 调派发红包
	 */
	public String payoutRedPacket(EcBpRedOrder ecBpRedOrder) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("celephoneNo", ecBpRedOrder.getPhone());
		params.put("dealId", ecBpRedOrder.getOrderId());
		params.put("platformId", ecBpRedOrder.getPlatformId());
		params.put("stockNumberId", ecBpRedOrder.getStockNumberId());
		String result = "";
		// TODO
		String flag = this.ecBpRedPacInvMapper.getParaValue("red.cmcc.assignredpacket.flag");
		
		if ("true".equals(flag) || StringUtils.isBlank(flag)) {
			result = redPacketForExtSysService.assignRedPacket(JSON.toJSONString(params));
		} else {
			result = "{\"dealId\": \"woyaohuijiale\",\"handleId\":66,\"isSuccess\": \"1\",\"responseCode\":\"0000\",\"responseDesc\": \"请求成功\"}";
		}
		com.alibaba.fastjson.JSONObject resultJson = JSON.parseObject(result);
		String isSuccess = (String) resultJson.get("isSuccess");
		return isSuccess;
	}

	/**
	 * 检验入参
	 * 
	 * @param req
	 * @return
	 */
	private Map<String, Object> checkReq(String req, String[] inputStr, String[] outputStr, String[] rightKeys, boolean isCheckPhone) {
		JSONObject resultJo = new JSONObject();
		/* 1.入参校验 */
		// 验证编码格式并解码请求字符串
		Map<String, Object> retMap = CommonUtil.decodeReqStr(req);
		if ("false".equals(retMap.get("isSuccess").toString())) {
			resultJo.put(outputStr[0], JdReturn.errorEncodeFormat.getCode());
			resultJo.put(outputStr[1], JdReturn.errorEncodeFormat.getMsg());
			log.info("==================调用通知商户下单接口end【" + resultJo.toString() + "】==================");
			retMap.put("msg", resultJo);
			return retMap;
		}
		String srcReqStr = retMap.get("srcReqStr").toString(); // 请求明文
		// 验证是否为合作商发出的请求
		Map<String, Object> ret1Map = isCorrectReq(srcReqStr);//srcReqStr解密后的报文
		if ("false".equals(ret1Map.get("isSuccess").toString())) {
			resultJo.put(outputStr[0], JdReturn.orderMerchantNotMatch.getCode());
			resultJo.put(outputStr[1], JdReturn.orderMerchantNotMatch.getMsg());
			log.info("==================调用通知商户下单接口end【" + resultJo.toString() + "】==================");
			retMap.put("isSuccess", false);
			retMap.put("msg", resultJo);
			return retMap;
		}
		// 是否是JSON格式
		String jsonStr = ret1Map.get("jsonStr").toString(); // 请求参数
		if (!CommonUtil.isJsonStr(jsonStr)) {
			resultJo.put(outputStr[0], JdReturn.isNotJson.getCode());
			resultJo.put(outputStr[1], JdReturn.isNotJson.getMsg());
			log.info("==================调用通知商户下单接口end【" + resultJo.toString() + "】==================");
			retMap.put("isSuccess", false);
			retMap.put("msg", resultJo);
			return retMap;
		}
		JSONObject jo = JSONObject.fromObject(jsonStr);
		// 参数名称校验
		if (!CommonUtil.checkKeyName(inputStr, jo)) {
			resultJo.put(outputStr[0], JdReturn.keyError.getCode());
			resultJo.put(outputStr[1], JdReturn.keyError.getMsg());
			log.info("==================调用通知商户下单接口end【" + resultJo.toString() + "】==================");
			retMap.put("isSuccess", false);
			retMap.put("msg", resultJo);
			return retMap;
		}
		String identityId = jo.getString(inputStr[2]);// 合作商户号
		JSONObject dataBody = jo.getJSONObject(inputStr[3]); // 请求体
		if (StringUtils.isBlank(identityId) || dataBody.isNullObject() || dataBody.isEmpty()) {
			resultJo.put(outputStr[0], JdReturn.keyError.getCode());
			resultJo.put(outputStr[1], JdReturn.keyError.getMsg());
			log.info("==================调用通知商户下单接口end【" + resultJo.toString() + "】==================");
			retMap.put("isSuccess", false);
			retMap.put("msg", resultJo);
			return retMap;
		}
		// 请求体必要字段校验;手机号格式校验
		if (!CommonUtil.checkNecessaryKey(rightKeys, dataBody)) {
			resultJo.put(outputStr[0], JdReturn.keyError.getCode());
			resultJo.put(outputStr[1], JdReturn.keyError.getMsg());
			log.info("==================调用通知商户下单接口end【" + resultJo.toString() + "】==================");
			retMap.put("isSuccess", false);
			retMap.put("msg", resultJo);
			return retMap;
		}
		if(isCheckPhone) {
			String mobile = dataBody.getString("phone");
			if (!CommonUtil.checkMobile(mobile)) {
				resultJo.put(outputStr[0], JdReturn.isNotJson.getCode());
				resultJo.put(outputStr[1], JdReturn.isNotJson.getMsg());
				log.info("==================调用通知商户下单接口end【" + resultJo.toString() + "】==================");
				retMap.put("isSuccess", false);
				retMap.put("msg", resultJo);
				return retMap;
			}
		}
		retMap.put("isSuccess", true);
		retMap.put("msg", jo);
		return retMap;
	}
	
	/**
	 * 验证是否为合作商发出的请求
	 * 
	 * @param reqStr
	 * @return
	 */
	private Map<String, Object> isCorrectReq(String reqStr) {
		log.info("待验证明文：" + reqStr);
		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("isSuccess", false);
		try {
			int cutLen = reqStr.indexOf("{");
			String sign_req = reqStr.substring(0, cutLen);
			String jsonStr = reqStr.substring(cutLen, reqStr.length());
			// sign = downcase(md5(secretKey+排序后json))
			char[] chs = jsonStr.toCharArray();
			Arrays.sort(chs);
			String sortStr = new String(chs);
			String secretKey = this.ecBpRedPacInvMapper.getParaValue("site.mall.red.cmcc.secretKey");

			String sign_m = MD5Util.getSign(secretKey + sortStr);
			if (sign_req.equals(sign_m)) {
				retMap.put("isSuccess", true);
				retMap.put("jsonStr", jsonStr);
			}
		} catch (Exception e) {
			log.error("");
		}
		return retMap;
	}

}
